﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Module;
using System.Data.SqlClient;
using Qmaker.DAL;
using System.Data;

namespace DLL
{
    public class DRelationPaper
    {
        #region SQL语句
        private const string SQL_INSERT = "insert into RelationPaper(RelationPaperId,UserRelationId,PaperId,DateCreated) values (@RelationPaperId,@UserRelationId,@PaperId,@DateCreated)";

        private const string SQL_UPDATE = "update RelationPaper set UserRelationId=@UserRelationId,PaperId=@PaperId,DateCreated=@DateCreated where RelationPaperId=@RelationPaperId ";

        private const string SQL_DELETE = "delete from RelationPaper where UserRelationId=@UserRelationId ";

        private const string SQL_DELETE_ID = "delete from RelationPaper where PaperId=@PaperId ";

        


        #endregion

        #region 公共接口
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="relationPaperInfo"></param>
        /// <returns></returns>
        public bool Insert(RelationPaperInfo relationPaperInfo)
        {
            if (relationPaperInfo == null)
                throw new ArgumentNullException("relationPaperInfo");

            SqlParameter[] parms = GetInsertParms(relationPaperInfo);
            //parms = SetInsertParmsCheck(parms);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        Convert.ToInt32(SqlHelper.ExecuteScalar(trans, CommandType.Text, SQL_INSERT, parms));
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="relationPaperInfo"></param>
        /// <returns></returns>
        public bool Update(RelationPaperInfo relationPaperInfo)
        {
            if (relationPaperInfo == null || relationPaperInfo.RelationPaperId.ToString().Length != 18)
                throw new ArgumentNullException("relationPaperInfo");

            SqlParameter[] parms = GetInsertParms(relationPaperInfo);

            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        SqlHelper.ExecuteNonQuery(trans, CommandType.Text, SQL_UPDATE, parms);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        throw;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 根据会员关系ID删除相关会员动态文章关系表
        /// </summary>
        /// <param name="userRelationId"></param>
        /// <returns></returns>
        public int Delete(long userRelationId)
        {
            SqlParameter[] parms = {
                    new SqlParameter("@UserRelationId",SqlDbType.BigInt)
                };
            parms[0].Value = userRelationId;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, SQL_DELETE, parms);

        }
        /// <summary>
        /// 根据文章Id删除会员动态关系文章
        /// </summary>
        /// <param name="paperId"></param>
        /// <returns></returns>
        public int DeleteByPaPerId(long paperId)
        {
            SqlParameter[] parms = {
                    new SqlParameter("@PaperId",SqlDbType.BigInt)
                };
            parms[0].Value = paperId;

            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, CommandType.Text, SQL_DELETE_ID, parms);

        }

        #endregion


        #region 私有接口

        private SqlParameter[] SetInsertParmsCheck(SqlParameter[] parms)
        {
            for (int i = 0; i < parms.Length; i++)
            {
                if (parms[i].Value == null)
                {
                    parms[i].Value = DBNull.Value;
                }
                if (parms[i].DbType == DbType.DateTime && parms[i].Value.Equals(DateTime.MinValue))
                {
                    parms[i].Value = DBNull.Value;
                }
            }
            return parms;
        }

        private SqlParameter[] GetInsertParms(RelationPaperInfo model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@RelationPaperId", SqlDbType.BigInt,8),
					new SqlParameter("@UserRelationId", SqlDbType.BigInt,8),
					new SqlParameter("@PaperId", SqlDbType.BigInt,8),
					new SqlParameter("@DateCreated", SqlDbType.DateTime)};
            parameters[0].Value = model.RelationPaperId;
            parameters[1].Value = model.UserRelationId;
            parameters[2].Value = model.PaperId;
            parameters[3].Value = model.DateCreated;

            return SetInsertParmsCheck(parameters);
        }



        #endregion
    }
}
